<?php

App::uses('AppController', 'Controller');

class CategoriesController extends AppController {

    private function __categories($slug = null) {
        $this->helpers[] = 'Tree';
        $options_categories = array(
            'recursive' => -1,
            'order' => array(
                'Category.lft' => 'ASC'
            ),
            'conditions' => array(
            ),
        );
        
        $categories = $this->Category->find('all', $options_categories);

        $settings = array(
            'limit' => 12,
            'recursive' => -1,
            'order' => array(
                'Product.name' => 'ASC'
            ),
        );

        if ($slug) {
            $options_categories['conditions']['Category.slug'] = $slug;
            $category = $this->Category->find('first', $options_categories);
            if (empty($category)) {
                return $this->redirect(array('action' => 'index'));
            }
            $this->set(compact('category'));

            $parents = $this->Category->getPath($category['Category']['id']);
            $this->set(compact('parents'));
            $directChildren = $this->Category->children($category['Category']['id']);
            $directChildrenIds = Hash::extract($directChildren, '{n}.Category.id');
            array_push($directChildrenIds, $category['Category']['id']);

            $settings['conditions']['Product.category_id'] = $directChildrenIds;
        }
        $this->Paginator->settings = $settings;
        $products = $this->Paginator->paginate($this->Category->Product);
        $this->set(compact('categories'));
        $this->set(compact('products'));
    }

    public function index() {
        $this->__categories();
    }

    public function view($slug = null) {
        $this->__categories($slug);
    }

    /* -------------------------------- Administração ----------------------------------------------------- */

    protected function __list() {
        $parents = $this->Category->generateTreeList(null, null, null, ' -- ');
        $this->set(compact('parents'));
    }

    /**
     * Lista todos os "usuarios".
     *
     * @method        admin_index
     * @version       1.0.0
     * @return void
     */
    public function admin_index() {
        parent::admin_index();
    }

    /**
     * Cadastrar "usuarios"
     *
     * @method        admin_add
     * @version       1.0.0
     * @return void
     */
    public function admin_add() {
        parent::admin_add();
    }

    /**
     * Alterar de acordo com o ID  "usuarios" especifico.
     *
     * @method        admin_edit
     * @version       1.0.0
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        parent::admin_edit($id);
    }

    /**
     * Excluir de acordo com o ID  "usuarios" especifico.
     *
     * @method        admin_delete
     * @version       1.0.0
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        parent::admin_delete($id);
    }

    /**
     * Exportar dados do banco em formato CSV da tabela "usuarios".
     *
     * @method        admin_export_data
     * @version       1.0.0
     * @return void
     */
    public function admin_export() {
        parent::admin_export();
    }

    /**
     * Detalhes do registro.
     *
     * @method        admin_view
     * @version       1.0.0
     * @return void
     */
    public function admin_view($id = null) {
        parent::admin_view($id);
    }

}
